﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Security.Permissions;
using Microsoft.Win32;
using System.Threading;

using nStruct;
using nFile;
using nGrid;
using nRS232;
using nUtil;

//Registry
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER")]

namespace LDS1000_V1
{
	public partial class frmMain : Form
	{

		public ENG_Struct ENG_Mode = new ENG_Struct();

		public Select_Struct Select_ITEM = new Select_Struct();

		public PS_Conf_Struct[] PS_Conf = new PS_Conf_Struct[3];

		public PS_Conf_Struct PS_Conf_temp = new PS_Conf_Struct();
		public Com_Conf_Struct Com_Conf = new Com_Conf_Struct();
		public Setup_Group setupGroup = new Setup_Group();
		Thread thread_rs232 = null;

		public frmMain()
		{
			InitializeComponent();
		}

		private void frmMain_Load(object sender, EventArgs e)
		{


			rbGroupA.Checked = true;
			cmbEngModuleNo.Items.Clear();
			for (int i = 0; i < 10; i++)
			{
				cmbEngModuleNo.Items.Add((i + 1).ToString());
			}
			cmbEngModuleNo.SelectedIndex = 0;

			cmbEngChannelNo.Items.Clear();
			for (int i = 0; i < 4; i++)
			{
				cmbEngChannelNo.Items.Add((i + 1).ToString());
			}
			cmbEngChannelNo.SelectedIndex = 0;

			numEngVoltage.Value = 0;
			numEngCurrent.Value = 0;
			chkEngStartStop.Checked = false;




			//PS_Conf_Struct[] PS_Conf = new PS_Conf_Struct[3];


			for (int i = 0; i < 3; i++) PS_Conf[i] = new PS_Conf_Struct();


			//string fName = Registry_Read();
			string fName = "D:\\LDS2000_Data\\Config_File.dat";

			if (fName != "")
			{
				Com_Conf = cFile.Config_File_Read(fName);

				for (int i = 0; i < 3; i++)
				{
					PS_Conf[i] = cFile.PowerSetup_FileOpen(Com_Conf.PS_Conf_FileName[i]);
					PS_Conf[i].FullFileName = Com_Conf.PS_Conf_FileName[i];
					PS_Conf[i].FolderName = cUtil.File_Seperate_From_FullName(0, Com_Conf.PS_Conf_FileName[i]);
					PS_Conf[i].FileName = cUtil.File_Seperate_From_FullName(1, Com_Conf.PS_Conf_FileName[i]);
					labConfFileName.Text = PS_Conf[i].FileName;
				}
			}
			else
			{

				Com_Conf.PortName[0] = "COM10";
				Com_Conf.PortName[1] = "COM1";
				Com_Conf.PortName[2] = "COM2";

				//default Setting
				labConfFileName.Text = "None";
				Default_Data_Initialize();
			}
			for (int i = 0; i < 3; i++)
			{
				PS_Conf[i].SelectItem = 0;
				PS_Conf[i].Calc_HW_Data(PS_Conf[i]);
			}

			Object_Create();


			for (int i = 0; i < 3; i++) Com_Conf.Speed[i] = 57600;
			Comport_Setup();

			if (Com_Conf.Status[0] == 1)
			{
				thread_rs232 = new Thread(new ThreadStart(RS232_Receive_Thread_Process));
				thread_rs232.Start();

				Device_Check();
			}

			for (int i = 0; i < 3; i++) PS_Conf[i].LogFolder = "D:\\LDS2000_Data\\data\\";


		
		}

		public void Comport_Setup()
		{
			//com port
			if (cRS232.Set_RS232(Com_Conf, 0, true) == Com_Conf.PortName[0])
			{
				btnRS232Status.Text = "Port(" + Com_Conf.PortName[0] + ") : O.K";
				Com_Conf.Status[0] = 1;//enable ok
			}
			else
			{
				btnRS232Status.Text = "Com Port Error";
				Com_Conf.Status[0] = 0;//Disable
			}

			cRS232.Set_RS232(Com_Conf, 1, true);
			cRS232.Set_RS232(Com_Conf, 2, true);
		}


		private void setupToolStripMenuItem_Click(object sender, EventArgs e)
		{

		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void Registry_Write(string inFileName)
		{
			cFile.Config_File_Write(inFileName, Com_Conf);

			RegistryKey regKey = Registry.CurrentUser.CreateSubKey("LDS2000");//create
			using (RegistryKey tName = regKey.CreateSubKey("FileName"))
			{
				tName.SetValue("fName", inFileName);// Create data for the TestSettings subkey.
			}
		}

		private string Registry_Read()
		{

			RegistryKey regCheck = Registry.CurrentUser;
			string[] regNames = regCheck.GetSubKeyNames();

			bool R_sts = false;
			for (int i = 0; i < regNames.Length; i++)
			{
				if (regNames[i] == "LDS2000")
				{
					R_sts = true;
					break;
				}
			}
			if (R_sts)
			{
				RegistryKey regKey = Registry.CurrentUser.CreateSubKey("LDS1000");//create
				RegistryKey tName = regKey.CreateSubKey("FileName");//sub create
				string[] subKeyName = regKey.GetSubKeyNames();
				RegistryKey tempKey = regKey.OpenSubKey(subKeyName[0]);
				string[] valueName = tempKey.GetValueNames();
				return Convert.ToString(tempKey.GetValue(valueName[0]));
			}
			else
			{
				return "";
			}
		}


		private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
		{
			//if (PS_Conf.FullFileName != "") Registry_Write(PS_Conf.FullFileName);
			//Registry_Write("D:\\LDS2000_Data\\Config_File.dat");

			if (thread_rs232 != null)
			{
				if (thread_rs232.IsAlive) thread_rs232.Abort();
			}

		}



		public void Object_Create()
		{
			cGrid.MainForm_PS_Grid_Create(gridPS);
			for (int i = 0; i < 3; i++) cGrid.MainForm_PS_Grid_Create_Name(i, gridPS, PS_Conf[i]);


			cGrid.MainForm_Temp_Grid_Create(gridTemp);
			for (int i = 0; i < 3; i++) cGrid.MainForm_Temp_Grid_Create_Name(i, gridTemp, PS_Conf[i]);


			for (int i = 0; i < 3; i++)
			{
				PS_Conf[i].SelectItem = 0;
				PS_Conf[i].PsSelectCh = 0;
			}


			Select_ITEM.PS_Group = 0;
			Select_ITEM.PS_CH = 0;
			Select_ITEM.PS_HW_CH = 0;
			Select_ITEM.Temp_Group = 0;
			Select_ITEM.Temp_Channel = 0;
			cGrid.MainForm_Information_Grid_Create(0, gridPSInfor);
			cGrid.MainForm_Information_Grid_Write(0, gridPSInfor, PS_Conf[Select_ITEM.PS_Group], Select_ITEM);
			cGrid.MainForm_Information_Grid_Write(1, gridPSInfor, PS_Conf[Select_ITEM.PS_Group], Select_ITEM);

			if (Com_Conf.Status[0] == 1) Hardware_Config_Setup_All();
		}
		private void Default_Data_Initialize()
		{

			float Iset = 200;
			float Vset = 100;

			for (int ik = 0; ik < 3; ik++)
			{
				PS_Conf[ik].FolderName = "";
				PS_Conf[ik].FullFileName = "";
				PS_Conf[ik].FileName = "";

				PS_Conf[ik].PsNo = 40;
				PS_Conf[ik].VsetMaster = Vset;
				PS_Conf[ik].IsetMaster = Iset;
				PS_Conf[ik].IProtect_Master = Iset + 50;

				PS_Conf[ik].TempNo = 16;
				PS_Conf[ik].TempProtectMaster = 80;

				for (int i = 0; i < PS_Conf[ik].PsNo; i++)
				{
					PS_Conf[ik].PsEn[i] = 1;
					PS_Conf[ik].PsName[i] = "PS " + (i + 1).ToString();
					PS_Conf[ik].Vset[i] = PS_Conf[ik].VsetMaster;
					PS_Conf[ik].Iset[i] = PS_Conf[ik].IsetMaster;
					PS_Conf[ik].IProtect[i] = PS_Conf[ik].IProtect_Master;
				}
				for (int i = 0; i < PS_Conf[ik].TempNo; i++)
				{
					PS_Conf[ik].TempEn[i] = 1;
					PS_Conf[ik].TempName[i] = "Temp " + (i + 1).ToString();
					PS_Conf[ik].TempProtect[i] = PS_Conf[ik].TempProtectMaster;
				}
			}
		}


		private void btnLogFileOpen_Click(object sender, EventArgs e)
		{
			/*if (PS_Conf.FolderName != "") folderDlg.SelectedPath = PS_Conf.FolderName;
			folderDlg.SelectedPath = "";
			folderDlg.ShowDialog();

			if (folderDlg.SelectedPath != "")
			{
				PS_Conf.FolderName = folderDlg.SelectedPath;
				labLogFolder.Text = PS_Conf.FolderName;
			}
			 * */
		}



		private void gridPS_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
		{

			if (gridPS == null) return;
			int no = 0;
			int row = gridPS.CurrentCellInfo.RowIndex;
			int col = gridPS.CurrentCellInfo.ColIndex;
			no = (row - 1) / 2 * 10 + (col - 1);
			if (row == 0 || no < 0 || col < 1) return;


			if (row < 9)
			{
				Select_ITEM.PS_Group = 0;

			}
			else if (row < 17)
			{
				Select_ITEM.PS_Group = 1;
				no = no - 40;
			}
			else
			{
				Select_ITEM.PS_Group = 2;
				no = no - 40 * 2;
			}


			Select_ITEM.PS_CH = no;
			Select_ITEM.PS_HW_CH = no;

			cGrid.MainForm_Information_Grid_Write(0, gridPSInfor, PS_Conf[Select_ITEM.PS_Group], Select_ITEM);

		}

		private void gridTemp_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
		{
			if (gridPS == null) return;
			int row = gridTemp.CurrentCellInfo.RowIndex;
			int col = gridTemp.CurrentCellInfo.ColIndex;

			int no = (row - 1) / 2 * 8 + (col - 1);
			if (row == 0 || no < 0 || col < 1) return;


			if (row < 5)
			{
				Select_ITEM.Temp_Group = 0;

			}
			else if (row < 9)
			{
				Select_ITEM.Temp_Group = 1;
				no = no - 16;
			}
			else
			{
				Select_ITEM.Temp_Group = 2;
				no = no - 16 * 2;
			}

			Select_ITEM.Temp_Channel = no;

			cGrid.MainForm_Information_Grid_Write(1, gridPSInfor, PS_Conf[Select_ITEM.PS_Group], Select_ITEM);

		}

		private void btnRS232Status_Click(object sender, EventArgs e)
		{
			cRS232.Set_RS232(Com_Conf, 0, false);
			this.Enabled = false;
			frmRS232Setup fRS232Setup = new frmRS232Setup();
			fRS232Setup.Show(this);

		}

		private void tmRS232_Tick(object sender, EventArgs e)
		{
			for (int i = 0; i < 3; i++)
			{
				if (cRS232.ReceiveFlag[i] == 1)
				{
					cRS232.ReceiveFlag[i] = 0;
					RS232_Receive_Process(i);
				}
			}
		}

		private void RS232_Receive_Thread_Process()
		{

			while (true)
			{
				for (int ch = 0; ch < cRS232.MaxCH; ch++)
				{

					if (cRS232.msc[ch].IsOpen == true)
					{
						if (cRS232.msc[ch].BytesToRead != 0)
						{
							for (int i = 0; i < cRS232.msc[ch].BytesToRead; i++)
							{
								cRS232.BUF[ch, cRS232.Head[ch]++] = (byte)cRS232.msc[ch].ReadByte();
								cRS232.Head[ch] = cRS232.Head[ch] % cRS232.MaxBuffer;
							}

						}

						if (cRS232.Head[ch] != cRS232.Tail[ch])
						{
							if (cRS232.CMD_MessageFromDevice(ch) == true)
							{
								cRS232.CMD[ch] = cRS232.CMD_RxBuf[ch, 1];
								cRS232.size[ch] = cRS232.CMD_RxBuf[ch, 2] * 256 + cRS232.CMD_RxBuf[ch, 3];

								for (int i = 0; i < cRS232.size[ch]; i++) cRS232.data[ch, i] = cRS232.CMD_RxBuf[ch, i + 4];
								cRS232.ReceiveFlag[ch] = 1;
							}
						}
					}
				}
			}
		}

		private void RS232_Receive_Process(int inCH)
		{
			char[] cName = new char[20];
			int add = 0;
			byte[] inB = new byte[4];
			int no;

			switch (cRS232.CMD[inCH])
			{
				case cRS232CMD.cmdReceiveOKReturn:
					Com_Conf.ReceiveOK = true;
					break;
				case cRS232CMD.cmdStartStopQueryReturn:

					break;
				case cRS232CMD.cmdStatusAllQueryReturn:
					add = 0;
					for (int i = 0; i < PS_Conf[inCH].HW_No; i++)
					{
						PS_Conf[inCH].HW_I_Status[i] = (int)cRS232.data[inCH, add++];
						for (int j = 0; j < 2; j++) inB[j] = cRS232.data[inCH, add++];

						PS_Conf[inCH].HW_I_Value[i] = cUtil.Data_Convert_from_BYTE_Array_to_Float(1, inB);

					}
					for (int i = 0; i < PS_Conf[inCH].TempNo; i++)
					{
						PS_Conf[inCH].Temp_Value[i] = cRS232.data[inCH, add++];
					}

					cGrid.Calc_Warning_and_Protection_Status(PS_Conf[inCH]);
					cGrid.MainForm_PS_Grid_Write(inCH, gridPS, PS_Conf[inCH]);
					cGrid.MainForm_Temp_Grid_Write(inCH, gridTemp, PS_Conf[inCH]);

					//cGrid.MainForm_Information_Grid_Write_Monitor(PS_Conf.SelectItem, gridPSInfor, PS_Conf);

					Com_Conf.ReceiveOK = true;

					//if (PS_Conf.LogFileExist) cFile.Log_Data_Append(PS_Conf);

					break;

				case cRS232CMD.cmdPsConfigSetQueryReturn:

					no = Convert.ToInt32(cRS232.data[0, add++]);
					cName = new char[no];
					for (int i = 0; i < no; i++) cName[i] = Convert.ToChar(cRS232.data[inCH, add++]);
					PS_Conf_temp.FileName = cUtil.CharArray_to_String(no, cName);

					PS_Conf_temp.PsNo = Convert.ToInt32(cRS232.data[inCH, add++]);

					for (int i = 0; i < 2; i++) inB[i] = cRS232.data[inCH, add++];
					PS_Conf_temp.VsetMaster = cUtil.Data_Convert_from_BYTE_Array_to_Float(inCH, inB);

					for (int i = 0; i < 2; i++) inB[i] = cRS232.data[inCH, add++];
					PS_Conf_temp.IsetMaster = cUtil.Data_Convert_from_BYTE_Array_to_Float(1, inB);

					for (int i = 0; i < 2; i++) inB[i] = cRS232.data[inCH, add++];
					PS_Conf_temp.IProtect_Master = cUtil.Data_Convert_from_BYTE_Array_to_Float(1, inB);

					for (int i = 0; i < PS_Conf_temp.PsNo; i++)
					{
						PS_Conf_temp.PsEn[i] = Convert.ToInt32(cRS232.data[inCH, add++]);
						PS_Conf_temp.StringNo[i] = Convert.ToInt32(cRS232.data[inCH, add++]);

						for (int j = 0; j < 2; j++) inB[j] = cRS232.data[inCH, add++];
						PS_Conf_temp.Vset[i] = cUtil.Data_Convert_from_BYTE_Array_to_Float(inCH, inB);

						for (int j = 0; j < 2; j++) inB[j] = cRS232.data[inCH, add++];
						PS_Conf_temp.Iset[i] = cUtil.Data_Convert_from_BYTE_Array_to_Float(1, inB);

						for (int j = 0; j < 2; j++) inB[j] = cRS232.data[inCH, add++];
						PS_Conf_temp.IProtect[i] = cUtil.Data_Convert_from_BYTE_Array_to_Float(1, inB);

						no = Convert.ToInt32(cRS232.data[inCH, add++]);
						cName = new char[no];
						for (int j = 0; j < no; j++) cName[j] = Convert.ToChar(cRS232.data[0, add++]);
						PS_Conf_temp.PsName[i] = cUtil.CharArray_to_String(no, cName);
					}
					Com_Conf.ReceiveOK = true;
					break;
				case cRS232CMD.cmdTempConfigSetQueryReturn:

					PS_Conf_temp.TempNo = Convert.ToInt32(cRS232.data[inCH, add++]);
					PS_Conf_temp.TempProtectMaster = cRS232.data[inCH, add++];


					for (int i = 0; i < PS_Conf_temp.TempNo; i++)
					{
						PS_Conf_temp.TempEn[i] = Convert.ToInt32(cRS232.data[inCH, add++]);
						PS_Conf_temp.TempProtect[i] = cRS232.data[inCH, add++];

						no = Convert.ToInt32(cRS232.data[inCH, add++]);
						cName = new char[no];
						for (int j = 0; j < no; j++) cName[j] = Convert.ToChar(cRS232.data[inCH, add++]);
						PS_Conf_temp.TempName[i] = cUtil.CharArray_to_String(no, cName);
					}
					Com_Conf.ReceiveOK = true;
					break;
			}
		}
		private void Send_Receive_OK_Message()
		{
			cRS232.RS232Command(0, cRS232CMD.cmdReceiveOKReturn);
		}
		private void btnStart_Click(object sender, EventArgs e)
		{
			int sel_group = Select_ITEM.PS_Group;
			tmSend.Interval = 1000;
			if (Com_Conf.DeviceOK)
			{

				PS_Conf[sel_group].LogFileExist = false;

				DialogResult dr = MessageBox.Show("Log Data를 저장하시겠습니까?", "Log Data Save 알림", MessageBoxButtons.YesNo);
				if (dr == DialogResult.Yes)
				{

					saveDlg.Title = "LDS1000 Log Data Save File";
					saveDlg.Filter = "(*.dat)|*.dat";
					saveDlg.FileName = "";
					saveDlg.ShowDialog();
					string nFile = "";
					if (saveDlg.FileName != "")
					{
						nFile = saveDlg.FileName;

						PS_Conf[sel_group].LogFolder = cUtil.File_Seperate_From_FullName(0, nFile);
						PS_Conf[sel_group].LogFileName = cUtil.File_Seperate_From_FullName(1, nFile);
						labLogFileName.Text = PS_Conf[sel_group].LogFileName;

						cFile.Log_Data_Making_Header(PS_Conf[sel_group]);
						PS_Conf[sel_group].LogFileExist = true;
						 
					}

				}
				tmSend.Enabled = true;
				cRS232CTL.Start_Stop(1);

			}

		}

		private void btnStop_Click(object sender, EventArgs e)
		{
			if (Com_Conf.DeviceOK) cRS232CTL.Start_Stop(0);
			tmSend.Enabled = false;
		}


		public void Object_Copy_from_2nd_Struct_to_PS_Config(int inCH)
		{
			PS_Conf[inCH].FileName = PS_Conf_temp.FileName;
			PS_Conf[inCH].FullFileName = PS_Conf[inCH].FolderName + PS_Conf[inCH].FileName;
			PS_Conf[inCH].PsNo = PS_Conf_temp.PsNo;
			PS_Conf[inCH].IProtect_Master = PS_Conf_temp.IProtect_Master;
			for (int i = 0; i < PS_Conf_temp.PsNo; i++)
			{
				PS_Conf[inCH].PsName[i] = PS_Conf_temp.PsName[i];
				PS_Conf[inCH].PsEn[i] = PS_Conf_temp.PsEn[i];
				PS_Conf[inCH].StringNo[i] = PS_Conf_temp.StringNo[i];
				PS_Conf[inCH].Vset[i] = PS_Conf_temp.Vset[i];
				PS_Conf[inCH].Iset[i] = PS_Conf_temp.Iset[i];
				PS_Conf[inCH].IProtect[i] = PS_Conf_temp.IProtect[i];
			}

			PS_Conf[inCH].TempNo = PS_Conf_temp.TempNo;
			for (int i = 0; i < PS_Conf_temp.TempNo; i++)
			{
				PS_Conf[inCH].TempEn[i] = PS_Conf_temp.TempEn[i];
				PS_Conf[inCH].TempName[i] = PS_Conf_temp.TempName[i];
				PS_Conf[inCH].TempProtect[i] = PS_Conf_temp.TempProtect[i];
			}
			PS_Conf[inCH].Calc_HW_Data(PS_Conf[inCH]);
		}

		private void button1_Click(object sender, EventArgs e)
		{

			if (Hardware_Config_Setup_All() == false) Device_Check();
		}

		public bool Hardware_Config_Setup_All()
		{
			if (Com_Conf.DeviceOK)
			{
				int wTime = 30000;//for test
				cRS232CTL.PowerSupply_Config_Setup(PS_Conf[0]);
				if (Receive_OK_Wait(wTime) == false) return false;

				cRS232CTL.Temperature_Config_Setup(PS_Conf[0]);
				if (Receive_OK_Wait(wTime) == false) return false;
				return true;
			}
			else return false;

		}

		private bool Receive_OK_Wait(int inTime)
		{
			Com_Conf.ReceiveOK = false;
			int wcnt = 0;
			while (Com_Conf.ReceiveOK == false)
			{
				cUtil.mDelay_ms(10);
				wcnt++;
				if (wcnt >= inTime / 10) return false;
			}
			Com_Conf.ReceiveOK = false;
			return true;
		}



		private void tmSend_Tick(object sender, EventArgs e)
		{
			cRS232CTL.Query_Send(cRS232CMD.cmdStatusAllQuery);
		}

		private bool Config_Receive_All()
		{
			if (Com_Conf.DeviceOK)
			{
				int wTime = 1000;//for test
				cRS232CTL.Query_Send(cRS232CMD.cmdPsConfigSetQuery);
				if (Receive_OK_Wait(wTime) == false) return false;

				cRS232CTL.Query_Send(cRS232CMD.cmdTempConfigSetQuery);
				if (Receive_OK_Wait(wTime) == false) return false;

				Object_Copy_from_2nd_Struct_to_PS_Config(0);//0->ch 0
				Object_Create();
				return true;
			}
			else return false;

		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (Config_Receive_All() == false) Device_Check();
		}


		private void pSSetupOpenToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (setupGroup.GROUP == -1)
			{
				MessageBox.Show("선택된 그룹이 없습니다."); 
				return;
			}
			openDlg.Title = "LDS1000 Setup File Open";
			openDlg.Filter = "(*.psc)|*.psc";
			openDlg.FileName = "";
			openDlg.ShowDialog();


			/***************
			 * 
			 * 그룹별 파일 정보를 얻기 위해 다음을 수정 하였습니다.
			 *
			 * if (openDlg.FileName != "" )
			{

				PS_Conf[0] = cFile.PowerSetup_FileOpen(openDlg.FileName);
				PS_Conf[0].FullFileName = openDlg.FileName;
				PS_Conf[0].FolderName = cUtil.File_Seperate_From_FullName(0, PS_Conf[0].FullFileName);
				PS_Conf[0].FileName = cUtil.File_Seperate_From_FullName(1, PS_Conf[0].FullFileName);
				labConfFileName.Text = PS_Conf[0].FullFileName;
				PS_Conf[0].Calc_HW_Data(PS_Conf[0]);
				Object_Create();
			}
			 * 
			 * *************/
			

			if (openDlg.FileName != "")
			{

				PS_Conf[setupGroup.GROUP] = cFile.PowerSetup_FileOpen(openDlg.FileName);
				PS_Conf[setupGroup.GROUP].FullFileName = openDlg.FileName;
				PS_Conf[setupGroup.GROUP].FolderName = cUtil.File_Seperate_From_FullName(0, PS_Conf[setupGroup.GROUP].FullFileName);
				PS_Conf[setupGroup.GROUP].FileName = cUtil.File_Seperate_From_FullName(1, PS_Conf[setupGroup.GROUP].FullFileName);
				labConfFileName.Text = PS_Conf[setupGroup.GROUP].FileName;
				PS_Conf[setupGroup.GROUP].Calc_HW_Data(PS_Conf[setupGroup.GROUP]);
				Object_Create();
			}
		}




		private void Device_Check()
		{
			Com_Conf.DeviceOK = true;
			cRS232CTL.Query_Send(cRS232CMD.cmdDeviceOKQuery);
			Com_Conf.DeviceOK = Receive_OK_Wait(5000);
			if (Com_Conf.DeviceOK) btnStatusDeviceCheck.Text = "Device OK";
			else btnStatusDeviceCheck.Text = "Device not found";
		}


		private void btnStatusDeviceCheck_ButtonClick(object sender, EventArgs e)
		{
			Device_Check();
		}

		private void logDataSaveToolStripMenuItem_Click(object sender, EventArgs e)
		{

		}

		private void button3_Click(object sender, EventArgs e)
		{
			cRS232CTL.Query_Send(cRS232CMD.cmdStatusAllQuery);
		}

		private void comboBoxAdv1_Click(object sender, EventArgs e)
		{


		}


		private void gridPSInfor_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
		{

		}

		private void button4_Click(object sender, EventArgs e)
		{
			if (setupGroup.GROUP == -1)
			{
				MessageBox.Show("Setup 할 Group을 입력해주십시오.!");
				return;
			}

			//this.Visible = false;
			this.Enabled = false;
			frmSetup fSetup = new frmSetup(setupGroup);
			fSetup.Show(this);
		}

		private void frmMain_MaximumSizeChanged(object sender, EventArgs e)
		{

		}

		private void frMain_MaximumSizeChanged(object sender, EventArgs e)
		{

		}

		private void rbGroupA_CheckedChanged(object sender, EventArgs e)
		{
			ENG_Mode.Group = 0;

		}

		private void rbGroupB_CheckedChanged(object sender, EventArgs e)
		{
			ENG_Mode.Group = 1;
		}

		private void rbGroupC_CheckedChanged(object sender, EventArgs e)
		{
			ENG_Mode.Group = 2;
		}

		private void cmbEngModuleNo_SelectedIndexChanged(object sender, EventArgs e)
		{
			ENG_Mode.ModuleNo = cmbEngModuleNo.SelectedIndex;
			
		}

		private void cmbEngChannelNo_SelectedIndexChanged(object sender, EventArgs e)
		{
			ENG_Mode.ChannelNo = cmbEngChannelNo.SelectedIndex;
		}

		private void numEngVoltage_ValueChanged(object sender, EventArgs e)
		{
			ENG_Mode.Volt = (float)numEngVoltage.Value;
			cRS232CTL.RS232_ENGMODE_Control(ENG_Mode);
			
		}

		private void groupPSControl_Enter(object sender, EventArgs e)
		{
			
		}

		private void numEngCurrent_ValueChanged(object sender, EventArgs e)
		{
			ENG_Mode.Current = (float)numEngCurrent.Value;
			cRS232CTL.RS232_ENGMODE_Control(ENG_Mode);
		}

		private void chkEngStartStop_CheckedChanged(object sender, EventArgs e)
		{
			if (chkEngStartStop.Checked) ENG_Mode.OnOff = 1;
			else ENG_Mode.OnOff = 0;
			cRS232CTL.RS232_ENGMODE_Control(ENG_Mode);

		}

		private void radioButton3_CheckedChanged(object sender, EventArgs e)
		{
			setupGroup.GROUP = 0;
			labConfFileName.Text = PS_Conf[setupGroup.GROUP].FileName;
		}

		private void RsGroupB_CheckedChanged(object sender, EventArgs e)
		{
			setupGroup.GROUP = 1;
			labConfFileName.Text = PS_Conf[setupGroup.GROUP].FileName;
		}

		private void RsGroupC_CheckedChanged(object sender, EventArgs e)
		{
			setupGroup.GROUP = 2;
			labConfFileName.Text = PS_Conf[setupGroup.GROUP].FileName;
		}

		
	}
}
